/***************************************************************************
 *   Copyright (C) 2008 by Ryszard Krol   *
 *   rysiek.krol@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "picturelistwidgetitem.h"
#include <QDir>
#include <zlib.h>
#include <QDateTime>
#include <QTextCodec>
#include <QStringList>

#include "utils.h"

PictureListWidgetItem::PictureListWidgetItem(PictureListWidgetItem* item)
{
    this->shownName = item->getShownName();
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
    this->file =  item->getFile();
    this->commentString = item->getCommentString();
    this->setIconImage(new QImage(*item->getIconImage()));
    this->setKeywords(new QSet< QString >(*item->getKeywords()));
    this->setNote(item->getNote());
    this->setPlace(item->getPlace());
    this->setText(item->text());
    this->setIcon(item->icon());
    this->setTime( item->getTime());
    
}
/**
* Kostruktor Klasy
*/
PictureListWidgetItem::PictureListWidgetItem ( QFileInfo & fileInfo,
        QListWidget * parent )
        : QListWidgetItem ( parent )
{
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
    this->file =  fileInfo;
    this->commentString.clear();
    this->getDescription ( fileInfo );
    this->generateLabel();
    this->iconImage = '\0';
}

PictureListWidgetItem::PictureListWidgetItem(QFileInfo & fileInfo, QString name, QListWidget * parent ): QListWidgetItem ( parent )
{
    this->shownName = name;
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
    this->file =  fileInfo;
    this->commentString.clear();
    this->getDescription ( fileInfo );
    this->generateLabel();
    this->iconImage = '\0';


}
/**
* Destruktor Klasy
*/
PictureListWidgetItem::~PictureListWidgetItem()
{
    delete this->keywords;
    if(this->iconImage != '\0') delete this->iconImage;
}

void PictureListWidgetItem::getDescription ( const QFileInfo & fileInfo )
{
    this->keywords = new QSet<QString>();

    QFileInfo *descFile;
    QString dane = "";
    QDir commentDir ( fileInfo.dir() );
    this->commentString.clear();
    if ( commentDir.cd ( ".comments" ) )
    {
        descFile = new QFileInfo ( commentDir.absoluteFilePath (fileInfo.fileName() ) +".xml" );
        if ( descFile->exists() && !fileInfo.isDir())
        {
            if ( Utils::unGzip ( descFile, &dane ) )
            {
                this->commentString = dane;
                //Wyciaganie komentarza
                QString tekst = dane.section ("<Note>",1,1);
                tekst = tekst.section("</Note>", 0,0);
                this->note = tekst;

                //Wyciaganie miejsca
                tekst = dane.section("<Place>",1,1);
                tekst = tekst.section("</Place>",0,0);
                this->place = tekst;

                //WyciÃÂganie czasu
                tekst = dane.section("<Time>",1,1);
                tekst = tekst.section("</Time>",0,0);
                if (tekst != "")
                {
                    this->time.setTime_t(tekst.toUInt());
                }

                //Wyciaganie miejsca
                tekst = dane.section("<Keywords>",1,1);
                tekst = tekst.section("</Keywords>",0,0);
                QStringList lista = tekst.split(",", QString::SkipEmptyParts);
                QList<QString>::const_iterator i;
                for (i=lista.constBegin(); i != lista.constEnd(); i++)
                {
                    this->keywords->insert(*i);
                }


            }
            else
            {
                this->commentString.clear();
            }
        }
        else
        {
            this->commentString.clear();
        }
        delete descFile;
    }
    else
    {
        dane = fileInfo.fileName();
        this->commentString.clear();
    }
}



/**
* Generuje Opis i ToolTipa z danymi zdjÃÂcia
*/
void PictureListWidgetItem::generateLabel()
{

    QString opis;
    QString toolTip;
    if (this->file.isDir())
    {
        if (this->shownName.isNull())
        {
            opis =this->file.fileName();
        }
        else
        {
            opis = this->shownName;
        }

    }
    else
    {
        toolTip = QObject::tr("Picture data: ");
        toolTip.append("\n" + QObject::tr("File Name: ") + this->file.fileName());
        if (this->note != "")
        {
            toolTip.append("\n" + QObject::tr("Comment: ") + this->note);
        }
        if (this->place != "")
        {
            toolTip.append("\n" + QObject::tr("Place: ") + this->place);
            opis.append(this->cutString(this->place));
        }
        if (this->time.isValid() && this->time.toTime_t()!=0)
        {
            toolTip.append("\n" + QObject::tr("Time: ") + this->time.toString());
            opis.append("\n" + this->time.date().toString());
            opis.append("\n" + this->time.time().toString());

        }
    }
    if (opis == "")
    {
        opis = this->cutString(this->file.fileName());
        this->setText(opis);

    }
    else
    {
        this->setText(opis);


    }
    if (!toolTip.isEmpty())
    {
        this->setToolTip(toolTip);
    }
}


/**
* Returns comment's Note field
*/
QString PictureListWidgetItem::getNote() const
{
    return note;
}

bool PictureListWidgetItem::isDate()
{


    return !this->time.isNull();
}

/**
* Sets Picture note to the value
*/
void PictureListWidgetItem::setNote ( const QString& theValue )
{
    note = theValue;
}

/**
* Returns Place of picture
*/
QString PictureListWidgetItem::getPlace() const
{
    return place;
}

/**
* Ustawia komentaz "miejsce"
*/
void PictureListWidgetItem::setPlace ( const QString& theValue )
{
    place = theValue;
}

/**
* Zwraca Plik klasy
* @return QFileInfo - zwraca plik do ktÃÂ³rego odnosi siÃÂ plik
*/
QFileInfo  PictureListWidgetItem::getFile()
{
    return this->file;
}

/**
* Ustawia plik w klasie
* @param QFileInfo - plik do ktÃÂ³rego ta klasa ma siÃÂ odnosiÃÂ
*/
void PictureListWidgetItem::setFile ( const QFileInfo& theValue )
{
    file = theValue;
}

/**
* Zwraca Czas zrobienia zdjÃÂcia
* @return QDateTime - Data zrobienia zdjÃÂcia
*/
QDateTime PictureListWidgetItem::getDateTime()
{
    return this->time;

}


/**
* Zwraca commentString
* @return QString - string zawiera komentarz XML'owy
*/
QString PictureListWidgetItem::getCommentString() const
{
    return commentString;
}

/**
* Formatuje String do wyÃÂwietlania w broswerWidgecie
*/
QString PictureListWidgetItem::cutString(QString string)
{
    string = string.left(30);
    if (string.size() == 30) string.append("...");
    if (string.size()>17)
    {
        if (string.lastIndexOf(" ", 18)>0) return string.replace(string.lastIndexOf(" ", 18), 1, "\n");
        string.chop((20 - string.size())*(-1));
        string = string.append("...");

    }
    return string;

}


QString PictureListWidgetItem::getShownName() const
{
    return shownName;
}


void PictureListWidgetItem::setShownName(const QString& theValue)
{
    shownName = theValue;
}

QSet< QString >* PictureListWidgetItem::getKeywords() const
{
    return keywords;
}


void PictureListWidgetItem::setKeywords(QSet< QString >* theValue)
{
    keywords = theValue;
}

QImage* PictureListWidgetItem::getIconImage() const
{
    return iconImage;
}

void PictureListWidgetItem::setIconImage(QImage* theValue)
{
    iconImage = theValue;
}




QDateTime PictureListWidgetItem::getTime() const
{
    return time;
}


void PictureListWidgetItem::setTime(const QDateTime& theValue)
{
    time = theValue;
}
